{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 腐蚀与膨胀"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1. 腐蚀操作"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.1 汉字腐蚀(例)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2 #opencv的缩写为cv2\n",
    "import matplotlib.pyplot as plt # matplotlib库用于绘图展示\n",
    "import numpy as np   # numpy数值计算工具包\n",
    "\n",
    "# 魔法指令，直接展示图，Jupyter notebook特有\n",
    "%matplotlib inline   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 腐蚀操作通常是拿二值图像做腐蚀操作\n",
    "\n",
    "img = cv2.imread('01_Picture/05_Dige.png')\n",
    "cv2.imshow('img',img)\n",
    "cv2.waitKey(0)\n",
    "cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 只要框里有黑色，中心点的值就变为黑色，即原来的白色被黑色腐蚀掉\n",
    "\n",
    "kernel = np.ones((5,5),np.uint8)\n",
    "erosion = cv2.erode(img,kernel,iterations=1)\n",
    "\n",
    "cv2.imshow('erosion',erosion)\n",
    "cv2.waitKey(0)\n",
    "cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.2 圆形腐蚀(例)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "pie = cv2.imread('01_Picture/06_pie.png')\n",
    "cv2.imshow('pie',pie)\n",
    "cv2.waitKey(0)\n",
    "cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "kernel = np.ones((30,30),np.uint8)\n",
    "erosion_1 = cv2.erode(pie,kernel,iterations=1)\n",
    "erosion_2 = cv2.erode(pie,kernel,iterations=2)\n",
    "erosion_3 = cv2.erode(pie,kernel,iterations=3)\n",
    "res = np.hstack((erosion_1,erosion_2,erosion_3))\n",
    "cv2.imshow('res',res)\n",
    "cv2.waitKey(0)\n",
    "cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2. 膨胀操作"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2.1 汉字膨胀(例)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "img = cv2.imread('01_Picture/05_Dige.png')\n",
    "cv2.imshow('img',img)\n",
    "cv2.waitKey(0)\n",
    "cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 先腐蚀 后膨胀，抵消腐蚀造成的损害\n",
    "kernel = np.ones((3,3),np.uint8)\n",
    "dige_erosion = cv2.erode(img,kernel,iterations=1)     \n",
    "cv2.imshow('erosion',dige_erosion)\n",
    "cv2.waitKey(0)\n",
    "cv2.destroyAllWindows()\n",
    "\n",
    "kernel = np.ones((3,3),np.uint8)\n",
    "dige_dilate = cv2.dilate(dige_erosion,kernel,iterations=1)     \n",
    "cv2.imshow('dilate',dige_dilate)\n",
    "cv2.waitKey(0)\n",
    "cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2.2 圆形腐蚀(例)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "pie = cv2.imread('01_Picture/06_pie.png')\n",
    "\n",
    "kernel = np.ones((30,30),np.uint8)\n",
    "dilate_1 = cv2.dilate(pie,kernel,iterations=1)\n",
    "dilate_2 = cv2.dilate(pie,kernel,iterations=2)\n",
    "dilate_3 = cv2.dilate(pie,kernel,iterations=3)\n",
    "res = np.hstack((dilate_1,dilate_2,dilate_3))\n",
    "cv2.imshow('res',res)\n",
    "cv2.waitKey(0)\n",
    "cv2.destroyAllWindows()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.6.3",
   "language": "python",
   "name": "python3.6.3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
